

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>7.5. StringIO — Read and write strings as files &mdash; Python v2.7.8 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.7.8',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.7.8 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.7.8 documentation" href="../index.html" />
    <link rel="up" title="7. String Services" href="strings.html" />
    <link rel="next" title="7.7. textwrap — Text wrapping and filling" href="textwrap.html" />
    <link rel="prev" title="7.4. difflib — Helpers for computing deltas" href="difflib.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 
    

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="textwrap.html" title="7.7. textwrap — Text wrapping and filling"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="difflib.html" title="7.4. difflib — Helpers for computing deltas"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python v2.7.8 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="strings.html" accesskey="U">7. String Services</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-StringIO">
<span id="stringio-read-and-write-strings-as-files"></span><h1>7.5. <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-mod docutils literal"><span class="pre">StringIO</span></tt></a> &#8212; Read and write strings as files<a class="headerlink" href="#module-StringIO" title="Permalink to this headline">¶</a></h1>
<p>This module implements a file-like class, <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-class docutils literal"><span class="pre">StringIO</span></tt></a>, that reads and
writes a string buffer (also known as <em>memory files</em>).  See the description of
file objects for operations (section <a class="reference internal" href="stdtypes.html#bltin-file-objects"><em>File Objects</em></a>). (For
standard strings, see <a class="reference internal" href="functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a> and <a class="reference internal" href="functions.html#unicode" title="unicode"><tt class="xref py py-class docutils literal"><span class="pre">unicode</span></tt></a>.)</p>
<dl class="class">
<dt id="StringIO.StringIO">
<em class="property">class </em><tt class="descclassname">StringIO.</tt><tt class="descname">StringIO</tt><big>(</big><span class="optional">[</span><em>buffer</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#StringIO.StringIO" title="Permalink to this definition">¶</a></dt>
<dd><p>When a <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-class docutils literal"><span class="pre">StringIO</span></tt></a> object is created, it can be initialized to an existing
string by passing the string to the constructor. If no string is given, the
<a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-class docutils literal"><span class="pre">StringIO</span></tt></a> will start empty. In both cases, the initial file position
starts at zero.</p>
<p>The <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-class docutils literal"><span class="pre">StringIO</span></tt></a> object can accept either Unicode or 8-bit strings, but
mixing the two may take some care.  If both are used, 8-bit strings that cannot
be interpreted as 7-bit ASCII (that use the 8th bit) will cause a
<a class="reference internal" href="exceptions.html#exceptions.UnicodeError" title="exceptions.UnicodeError"><tt class="xref py py-exc docutils literal"><span class="pre">UnicodeError</span></tt></a> to be raised when <a class="reference internal" href="#StringIO.StringIO.getvalue" title="StringIO.StringIO.getvalue"><tt class="xref py py-meth docutils literal"><span class="pre">getvalue()</span></tt></a> is called.</p>
</dd></dl>

<p>The following methods of <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-class docutils literal"><span class="pre">StringIO</span></tt></a> objects require special mention:</p>
<dl class="method">
<dt id="StringIO.StringIO.getvalue">
<tt class="descclassname">StringIO.</tt><tt class="descname">getvalue</tt><big>(</big><big>)</big><a class="headerlink" href="#StringIO.StringIO.getvalue" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve the entire contents of the &#8220;file&#8221; at any time before the
<a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-class docutils literal"><span class="pre">StringIO</span></tt></a> object&#8217;s <a class="reference internal" href="#StringIO.StringIO.close" title="StringIO.StringIO.close"><tt class="xref py py-meth docutils literal"><span class="pre">close()</span></tt></a> method is called.  See the note above
for information about mixing Unicode and 8-bit strings; such mixing can cause
this method to raise <a class="reference internal" href="exceptions.html#exceptions.UnicodeError" title="exceptions.UnicodeError"><tt class="xref py py-exc docutils literal"><span class="pre">UnicodeError</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="StringIO.StringIO.close">
<tt class="descclassname">StringIO.</tt><tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#StringIO.StringIO.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Free the memory buffer.  Attempting to do further operations with a closed
<a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-class docutils literal"><span class="pre">StringIO</span></tt></a> object will raise a <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>.</p>
</dd></dl>

<p>Example usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">StringIO</span>

<span class="n">output</span> <span class="o">=</span> <span class="n">StringIO</span><span class="o">.</span><span class="n">StringIO</span><span class="p">()</span>
<span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;First line.</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="o">&gt;&gt;</span><span class="n">output</span><span class="p">,</span> <span class="s">&#39;Second line.&#39;</span>

<span class="c"># Retrieve file contents -- this will be</span>
<span class="c"># &#39;First line.\nSecond line.\n&#39;</span>
<span class="n">contents</span> <span class="o">=</span> <span class="n">output</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>

<span class="c"># Close object and discard memory buffer --</span>
<span class="c"># .getvalue() will now raise an exception.</span>
<span class="n">output</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="module-cStringIO">
<span id="cstringio-faster-version-of-stringio"></span><h1>7.6. <a class="reference internal" href="#module-cStringIO" title="cStringIO: Faster version of StringIO, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cStringIO</span></tt></a> &#8212; Faster version of <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-mod docutils literal"><span class="pre">StringIO</span></tt></a><a class="headerlink" href="#module-cStringIO" title="Permalink to this headline">¶</a></h1>
<p>The module <a class="reference internal" href="#module-cStringIO" title="cStringIO: Faster version of StringIO, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cStringIO</span></tt></a> provides an interface similar to that of the
<a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-mod docutils literal"><span class="pre">StringIO</span></tt></a> module.  Heavy use of <a class="reference internal" href="#StringIO.StringIO" title="StringIO.StringIO"><tt class="xref py py-class docutils literal"><span class="pre">StringIO.StringIO</span></tt></a> objects can be
made more efficient by using the function <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-func docutils literal"><span class="pre">StringIO()</span></tt></a> from this module
instead.</p>
<dl class="function">
<dt id="cStringIO.StringIO">
<tt class="descclassname">cStringIO.</tt><tt class="descname">StringIO</tt><big>(</big><span class="optional">[</span><em>s</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#cStringIO.StringIO" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a StringIO-like stream for reading or writing.</p>
<p>Since this is a factory function which returns objects of built-in types,
there&#8217;s no way to build your own version using subclassing.  It&#8217;s not
possible to set attributes on it.  Use the original <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-mod docutils literal"><span class="pre">StringIO</span></tt></a> module in
those cases.</p>
<p>Unlike the <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-mod docutils literal"><span class="pre">StringIO</span></tt></a> module, this module is not able to accept Unicode
strings that cannot be encoded as plain ASCII strings.</p>
<p>Another difference from the <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-mod docutils literal"><span class="pre">StringIO</span></tt></a> module is that calling
<a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-func docutils literal"><span class="pre">StringIO()</span></tt></a> with a string parameter creates a read-only object. Unlike an
object created without a string parameter, it does not have write methods.
These objects are not generally visible.  They turn up in tracebacks as
<tt class="xref py py-class docutils literal"><span class="pre">StringI</span></tt> and <tt class="xref py py-class docutils literal"><span class="pre">StringO</span></tt>.</p>
</dd></dl>

<p>The following data objects are provided as well:</p>
<dl class="data">
<dt id="cStringIO.InputType">
<tt class="descclassname">cStringIO.</tt><tt class="descname">InputType</tt><a class="headerlink" href="#cStringIO.InputType" title="Permalink to this definition">¶</a></dt>
<dd><p>The type object of the objects created by calling <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-func docutils literal"><span class="pre">StringIO()</span></tt></a> with a string
parameter.</p>
</dd></dl>

<dl class="data">
<dt id="cStringIO.OutputType">
<tt class="descclassname">cStringIO.</tt><tt class="descname">OutputType</tt><a class="headerlink" href="#cStringIO.OutputType" title="Permalink to this definition">¶</a></dt>
<dd><p>The type object of the objects returned by calling <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-func docutils literal"><span class="pre">StringIO()</span></tt></a> with no
parameters.</p>
</dd></dl>

<p>There is a C API to the module as well; refer to the module source for  more
information.</p>
<p>Example usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">cStringIO</span>

<span class="n">output</span> <span class="o">=</span> <span class="n">cStringIO</span><span class="o">.</span><span class="n">StringIO</span><span class="p">()</span>
<span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;First line.</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="o">&gt;&gt;</span><span class="n">output</span><span class="p">,</span> <span class="s">&#39;Second line.&#39;</span>

<span class="c"># Retrieve file contents -- this will be</span>
<span class="c"># &#39;First line.\nSecond line.\n&#39;</span>
<span class="n">contents</span> <span class="o">=</span> <span class="n">output</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>

<span class="c"># Close object and discard memory buffer --</span>
<span class="c"># .getvalue() will now raise an exception.</span>
<span class="n">output</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">7.5. <tt class="docutils literal"><span class="pre">StringIO</span></tt> &#8212; Read and write strings as files</a></li>
<li><a class="reference internal" href="#module-cStringIO">7.6. <tt class="docutils literal"><span class="pre">cStringIO</span></tt> &#8212; Faster version of <tt class="docutils literal"><span class="pre">StringIO</span></tt></a></li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="difflib.html"
                        title="previous chapter">7.4. <tt class="docutils literal docutils literal"><span class="pre">difflib</span></tt> &#8212; Helpers for computing deltas</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="textwrap.html"
                        title="next chapter">7.7. <tt class="docutils literal docutils literal"><span class="pre">textwrap</span></tt> &#8212; Text wrapping and filling</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/stringio.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="textwrap.html" title="7.7. textwrap — Text wrapping and filling"
             >next</a> |</li>
        <li class="right" >
          <a href="difflib.html" title="7.4. difflib — Helpers for computing deltas"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python v2.7.8 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="strings.html" >7. String Services</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2014, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Jun 29, 2014.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>